package com.kharthick.pwGame.players;

import java.awt.Graphics;
import java.awt.MediaTracker;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

//import quicktime.qd3d.math.Vector2D;

import com.kharthick.pwGame.map.Tile;
import com.kharthick.pwGame.map.tiles.DesertTile;
import com.kharthick.pwGame.map.tiles.ForestTile;
import com.kharthick.pwGame.map.tiles.GrassTile;
import com.kharthick.pwGame.map.tiles.MountainTile;
import com.kharthick.pwGame.map.tiles.WaterTile;
import com.kharthick.pwGame.units.*;

public class Player {
	private String name;
	private List<Unit> units;
	private List<Unit> deadUnits;
	
	public final int TILE_SIZE = 50;
	protected int columns;
	protected int rows;
	
	public Player(){
		units = new ArrayList();
		deadUnits = new ArrayList();
	}
	
	// Get starting positions from the map and place units
	public void PlaceStartingUnits(){
	
	}
	
	public static Unit createUnit(String type, Vector<Integer> pos, MediaTracker tracker){ //throws Exception{
		Unit unit = null;
		if(type.equals("soldier")) {
			 unit = new Soldier(pos, tracker);
		}
		else if(type.equals("archer")) {
			unit = new Archer(pos, tracker);
		}
		else if(type.equals("catapult")) {
			unit = new Catapult(pos, tracker);
		}
		else {
			System.out.println("creating unknown unit");
			unit = new UnknownUnit(pos, tracker);
			System.out.println("created unknown unit");
			//throw new Exception("Unrecognized unit.");
		}
		
		return unit;
	}
	
	// adds a unit to the list
	public void addUnit(Unit u) {
		units.add(u);
	}
	
	public void removeUnit(Unit u){
		units.remove(u);
	}
	
	public void addDeadUnit(Unit u){
		deadUnits.add(u);
	}
	
	public Unit getUnitByScreenPosition(int x, int y) {
		int col = x / TILE_SIZE;
		int row = y / TILE_SIZE;
		
		Vector<Integer> click = new Vector();
		click.add(new Integer(col));
		click.add(new Integer(row));
		//System.out.println(col + " " + row);
		
		for(Iterator it = units.iterator(); it.hasNext(); ) {
			Unit u = (Unit) it.next();
			//System.out.println(u.getPosition().get(0) + " "
			//					+ u.getPosition().get(1));
			if(u.getPosition().equals(click)){
				return u;
			}
		}
		
		return null;
	}
	
	public void drawUnits(Graphics buffer) {
		for(Iterator it = units.iterator(); it.hasNext(); ) {
			Unit u = (Unit) it.next();
			u.draw(buffer);
		}
	}
	
	public void drawDeadUnits(Graphics buffer){
		for(Iterator it = deadUnits.iterator(); it.hasNext(); ) {
			Unit u = (Unit) it.next();
			u.draw(buffer);
		}
	}
}
